package ccpvspiral1;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

import repast.simphony.context.AbstractContext;
import repast.simphony.context.Context;
import repast.simphony.context.DefaultContext;
import repast.simphony.context.space.gis.GeographyFactoryFinder;
import repast.simphony.context.space.grid.GridFactoryFinder;
import repast.simphony.dataLoader.ContextBuilder;
import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.engine.schedule.ISchedule;
import repast.simphony.engine.schedule.ScheduleParameters;
import repast.simphony.space.gis.Geography;
import repast.simphony.space.gis.GeographyParameters;
import repast.simphony.space.gis.ShapefileLoader;
import repast.simphony.space.grid.GridBuilderParameters;
import repast.simphony.space.grid.RandomGridAdder;
import repast.simphony.space.grid.StickyBorders;
import java.io.*;
import java.net.MalformedURLException;
import java.util.Iterator;



public class GISContextCreator implements ContextBuilder<GisAgent> {


  public Context build(Context<GisAgent> context) {
  	
    GeographyParameters geoParams = new GeographyParameters();
    Geography geography = GeographyFactoryFinder.createGeographyFactory(null)
            .createGeography("Geography", context, geoParams);
    
    GridFactoryFinder.createGridFactory(null).createGrid("Grid", context,
            GridBuilderParameters.multiOccupancy2D(new RandomGridAdder(), new StickyBorders(),
                    30, 30));

    GeometryFactory fac = new GeometryFactory();  
    File shapefile = null; // NOTE: import java.io package!
    ShapefileLoader<GisAgent> personLoader = null;
    try {
    // CHANGE ME TO THE DIRECTORY OF SHAPEFILE... OR DON'T USE ME.
    shapefile = new File("C:/Users/Heather/Desktop/ccpv/People/People.shp");
    personLoader = new ShapefileLoader<GisAgent>(
    				GisAgent.class, shapefile.toURL(),
    				geography, context);
    }
    catch (java.net.MalformedURLException e) {
    e.printStackTrace();
    }
    while (personLoader.hasNext()) {
    personLoader.next();
    }
    
    RunEnvironment thisEnvironment = RunEnvironment.getInstance();
    ISchedule schedule = thisEnvironment.getCurrentSchedule();
    // Right now, it executes every second tick.  That's what the "2" is for.
		ScheduleParameters params = ScheduleParameters.createRepeating(1, 2);
		schedule.schedule(params, this, "step", context);
		
		return context;
  }
  
  public void step(Context context)
  {
  	/*
  	Iterator<GisAgent> agents = context.getAgentLayer(GisAgent.class).iterator();
  	while(agents.hasNext())
  	{
  		GisAgent current = agents.next();
  		System.out.println("current name: " + current.getName());
  	}
  	*/
  	
  	//System.out.println("HEY I'M THE CONTEXT STEP CALLED!!!!!");
  }
}
